package utils;
// Executes methods to the logic business
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class RingUtils {

	static RingUtils singleton;

	public static RingUtils getInstance () {
		if (singleton == null) {
			singleton = new RingUtils();
		}
		return singleton;
	}

	public List<BigInteger> generateToken(int n){

		List <BigInteger> newTokens = new ArrayList<BigInteger>();
		for (int i=0; i<=n-1;i++){
			double operation = (Math.pow(2, 127)/n*i);
			BigDecimal tokenV = new BigDecimal(operation);
			BigInteger token = tokenV.toBigInteger();
			if ( token.signum() == -1 )
				token = token.multiply(BigInteger.valueOf(-1L));
			newTokens.add(token);
		}
		return newTokens;

	}

	public List<Float> getLoadList(Map<String, String> loadMap) {


		List<Float> loadList = new ArrayList<Float>();

		for (String key: loadMap.keySet()){
			String load = loadMap.get(key);
			float num =  convertToBytes(load);
			loadList.add(num);
		}

		Collections.sort(loadList);
		return loadList;

	}


	private float convertToBytes (String load){
		String[] loadArray = load.split(" ");
		String number = loadArray[0];

		number = number.replace(',', '.');

		float num  = Float.valueOf(number);
		String unit = loadArray[1]; 
		char ut = unit.charAt(0);
		switch (ut)
		{
		case 'K':
			num = num*1024; 
			break;
		case 'M':
			num = num*1024*1024; 	 
			break;
		case 'G':
			num = num*1024*1024*1024; 	 
			break;

		}

		return num;
	}

}
